package cn.fl.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.condition.SysResourceQueryCondition;
import cmm.user.facade.system.dto.SysRoleResRDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysResourceService;
import cn.fl.system.service.ISysRoleResRService;
import cn.fl.system.transform.SysRoleResRVOTransform;
import cn.fl.system.vo.SysRoleResRVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SysRoleResRServiceImpl implements ISysRoleResRService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysRoleResRServiceImpl.class);
	
	/**
	 *  权限暴露服务
	 */
	@Autowired
	private IPriorityApiService priorityApiService;
	/**
	 *  资源服务
	 */
	@Autowired
	private ISysResourceService sysRoleResRServiceImpl;
	
	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public SysRoleResRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		SysRoleResRDTO dto = null;//this.自定义ApiService.selectById(id);
		return SysRoleResRVOTransform.toVO(dto);
	}


	
	

	@Override
	public JSONObject searchResRByRoleId(Long roleId,SysResourceQueryCondition condition) throws BaseException {
		
		List<Long> ids = this.priorityApiService.searchResRIdListByRoleId(roleId);
		JSONArray jsonArray = sysRoleResRServiceImpl.searchResRTree(ids, null);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("data", jsonArray); 
		if(CollectionUtils.isNotEmpty(ids)){
			
			jsonObject.put("IDs", JSONArray.fromObject(ids)); 
		}else{
			jsonObject.put("IDs", new JSONArray()); 
		}
		return jsonObject;
	}

	@Override
	public JSONObject searchResRByResRIdList(List<Long> resId,SysResourceQueryCondition condition) throws BaseException {
		JSONArray jsonArray = sysRoleResRServiceImpl.searchResRTree(resId, condition);
		JSONObject jsonObject = new JSONObject();
		if(jsonArray==null){
			jsonObject.put("data", new JSONArray()); 
		}else{
			
			jsonObject.put("data", jsonArray); 
		}
		
		jsonObject.put("IDs", JSONArray.fromObject(resId)); 
		return jsonObject;
	}
	
	@Override
	public boolean 	modifyExtendsResR(Long curRoleId, Long extRoleId)
			throws BaseException {
		if(StringUtils.isEmpty(curRoleId)||StringUtils.isEmpty(extRoleId)){
			BaseLogUtils.info(logger, "modifyExtendsResR", "权限继承失败,缺少必要的主键条件:当前角色ID和继承角色ID.curRoleId:"+curRoleId+",extRoleId:"+extRoleId, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return  this.priorityApiService.modifyExtendsResR(curRoleId, extRoleId);
	}
	

	@Override
	public boolean addAuth(Long roleId,List<SysRoleResRVO> list) throws BaseException {
		if(StringUtils.isEmpty(roleId)){
			BaseLogUtils.info(logger, "addAuth", "授权失败,角色ID为空.roleId:"+roleId, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		if(CollectionUtils.isEmpty(list)){
			return this.priorityApiService.removeSysRoleResRByRoleId(roleId);
		}
		
	    return this.priorityApiService.addSysRoleResR(roleId, SysRoleResRVOTransform.toDTOList(list));
		
	}





	@Override
	public List<KeyValueVO> searchResTypCdList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_SYS_ROLE_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	/*@Override
	public boolean modify(SysRoleResRVO entity) throws BaseException{
		if(null == entity)return false;
		return this.sysRoleResRApiService.modify(SysRoleResRVOTransform.toDTO(entity));
	}*/

	/*@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.sysRoleResRApiService.searchListPage(page);
	}*/


	@Override
	public List<KeyValueVO> searchIsVisCdList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_SYS_YES_NO_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}
}
